package com.aaa.filter;

import org.apache.log4j.Logger;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * 监控日志 过滤器
 */
public class LogFilter implements Filter {

    Logger logger = Logger.getLogger(Logger.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

        logger.debug("LogFilter---init");

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 不进行任何鉴权过滤，只获取参数
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        String url =  request.getRequestURI();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(url+"?");

        // 获取所有请求参数的额键值对
        Map<String, String[]>  map = request.getParameterMap();

        for (String key:map.keySet()){

            String[] strings = map.get(key);
            for (String value:strings){
                stringBuilder.append(key+"="+value+"&");
            }
        }
        // http://localhost:8080/mvc_classs_QY08_20200325_war_exploded/GetStudentListByPage?pageindex=1&pagesize=5
        // ？ 分割的是 路径与参数
        // &  分割的是参数
        logger.info("请求内容:"+stringBuilder.toString());

        filterChain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {
        logger.debug("LogFilter---destroy");
    }
}
